package com.gxy.learn.thread.example;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description:
 * @author: gaoxueyong
 * @Date: 2022/11/10 11:07
 */
public class ReentrantLockTest {
    Lock lock = new ReentrantLock();

    void m1() {
        lock.lock();
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } finally {
            lock.unlock();
        }
    }

    void m2() {
        lock.lock();
        try {
            System.out.println("m2");

        } finally {
            lock.unlock();
        }
    }

    void m3() {
        lock.lock();
        try {

            for (int i = 10; i < 20; i++) {
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } finally {
            lock.unlock();
        }
    }


    public static void main(String[] args) {
        ReentrantLockTest tt = new ReentrantLockTest();
        new Thread(tt::m1).start();
        new Thread(tt::m2).start();
        new Thread(tt::m3).start();
    }
}
